<template>
  <div class="login-container">
    <div class="loginBox">
      <el-form
      class="login-form"
      autocomplete="on"
      :model="rigisterForm"
      :rules="rigisterRules"
      ref="rigisterForm"
      label-position="left">
        <div class="title-container">
          <div class="logoInfo clearfix">
            <em class="logo"></em>
            <span class="logoFontZ">黑马面面</span>
            <span>用户注册</span>
          </div>
        </div>
        <el-form-item prop="username">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user"/>
          </span>
          <el-input
            name="username"
            type="text"
            v-model="rigisterForm.username"
            autocomplete="on"
            placeholder="用户名"/>
        </el-form-item>
          <el-form-item prop="验证码" class="code">
            <el-input  v-model="sendcode" placeholder="请输入验证码"></el-input>
            <el-button type="button" @click="send" :disabled="disabled" v-if="disabled==false">发送验证码
            </el-button>
            <el-button type="button" @click="send" :disabled="disabled" v-if="disabled==true">{{btntxt}}
            </el-button>
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"/>
          </span>
          <el-input
            name="password"
            :type="passwordType"
            @keyup.enter.native="handleRegister"
            v-model="rigisterForm.password"
            autocomplete="on"
            placeholder="密码"/>
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye"/>
          </span>
        </el-form-item>

        <el-form-item prop="checkPass">
          <span class="svg-container">
            <svg-icon icon-class="password"/>
          </span>
          <el-input placeholder="确认密码" type="password" v-model="rigisterForm.checkPass" autocomplete="off"></el-input>
          <span class="show-pwd" @click="showPwd">
            <svg-icon icon-class="eye"/>
          </span>
        </el-form-item>

        <el-button
          type="primary"
          style="width:100%;margin-bottom:30px;"
          :loading="loading"
          @click.native.prevent="handleRegister">注册
        </el-button>
        <el-link type="primary" @click="toLogin">去登录</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validateEmail, validatePhone } from '@/utils/validate'
import { register, sendsms } from '@/api/base/frame'
import shajs from 'sha.js'

export default {
  data () {
    const validateUsername = (rule, value, callback) => {
      if (validateEmail(value) || validatePhone(value)) {
        callback()
      } else {
        callback(new Error('请输入邮箱或电话号码'))
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度需大于6位'))
      } else {
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.rigisterForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      rigisterForm: {
        username: '',
        password: '',
        checkPass: ''
      },
      disabled: false,
      sendcode: '',
      time: 0,
      btntxt: '重新发送',
      rigisterRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ],
        checkPass: [
          { trigger: 'blur', validator: validatePass2 }
        ]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    send () {
      const reg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/
      const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
      if (this.rigisterForm.username === '') {
        this.$message({
          message: '手机号不能为空',
          center: true
        })
        return
      }
      if (reg.test(this.rigisterForm.username) || re.test(this.rigisterForm.username)) {
        sendsms({
          username: this.rigisterForm.username
        }).then(response => {
          this.$message({
            message: response.data.success,
            type: 'success',
            center: true
          })
          if (response.data.code === 20000) {
            this.time = 60
            this.disabled = true
            this.timer()
          }
        }).catch(error => {
          console.log(error)
        })
      } else {
        this.$message({
          message: '请输入正确的手机号或邮箱号',
          center: true
        })
      }
    },
    // 60S倒计时
    timer () {
      if (this.time > 0) {
        this.time--
        this.btntxt = this.time + 's后重新获取'
        setTimeout(this.timer, 1000)
      } else {
        this.time = 0
        this.btntxt = '获取验证码'
        this.disabled = false
      }
    },
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    toLogin () {
      this.$router.push('/login')
    },
    handleRegister () {
      this.$refs.rigisterForm.validate(async valid => {
        if (valid) {
          this.loading = true
          const { username, password } = this.rigisterForm
          register({
            username: username,
            password: shajs('sha256')
              .update(password)
              .digest('hex'),
            code: this.sendcode
          }).then(response => {
            this.$message({
              message: response.data.success,
              type: 'success',
              center: true
            })
            this.loading = false
            if (response.data.code === 20000) {
              this.$router.push('/login')
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #333;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid #DCDFE6;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
  .el-button--medium{
    height: 50px;
    line-height: 20px;
    font-size: 22px;
  }
  .code {
    .el-input {
        width: 55%;
        border-radius: 0px;
        float: left;
        display: inline-block;
    }
    .el-button {
        width: 45%;
        border-top-left-radius: 0px;
        border-bottom-left-radius: 0px;
        border-left: 0px;
        float: left;
        display: inline-block;
    }
}
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #333;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;

  background: url('./../assets/bg.jpg');
  -moz-background-size: 100% 100%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .loginBox{
     height: 100%;
     width: 100%;
    background: url('./../assets/logingBg.png') no-repeat 100% 100%;
    position: relative;
  }
  .login-form {
    position: absolute;
    left: 20%;
    top: 50%;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: -200px 0 0 0;
    background:#f5f5f5;
  }
  .tips {
    font-size: 14px;
    // color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      // font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
.logoInfo{
  padding-bottom:15px;
}
.logoInfo span{
  font-size: 22px;
  padding: 0 10px;
  float: left;
}
.logoInfo .logo{
  background: url(../assets/loginLogo.png) no-repeat;
  display:inline-block;
  width: 35px;
  height: 26px;
  float: left;
}
</style>
